package com.ruoyi.project.stat.paeaReportWorkload.domain;

import com.ruoyi.common.enums.WorkloadType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StatisticsWorkloadWrapper implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2589769853570975776L;
	@Excel(name = "机构号")
	private String organCode;
	@Excel(name = "机构名")
	private String organName;
	@Excel(name = "操作员号")
	private String userCode;
	@Excel(name = "操作员名")
	private String userName;

	public StatisticsWorkloadWrapper(String organCode, String organName,
			String userCode, String userName) {

		this.organCode = organCode;
		this.organName = organName;
		this.userCode = userCode;
		this.userName = userName;
	}

	public static List<StatisticsWorkloadWrapper> toStatisticsWorkloadWrapperList(
			List<StatisticsWorkload> statisticsWorkloads, String statType) {
		List<StatisticsWorkloadWrapper> wrappers = new ArrayList<StatisticsWorkloadWrapper>();
		String key = null;
		StatisticsBpoWorkloadWrapper statisticsWorkloadWrapper = null;
		for (StatisticsWorkload statisticsWorkload : statisticsWorkloads) {
			String organTellerString = statisticsWorkload.getOrganTellerString();
			if (organTellerString.equals(key) == false) {
				key = organTellerString;
				statisticsWorkloadWrapper = new StatisticsBpoWorkloadWrapper(
						statisticsWorkload.getOrganCode(), 
						statisticsWorkload.getOrganName(), 
						statisticsWorkload.getUserCode(), 
						statisticsWorkload.getUserName());
				wrappers.add(statisticsWorkloadWrapper);
			}
			if (WorkloadType.扫描.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendScanWorkload(statisticsWorkload);
			} else if (WorkloadType.人工补录.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendInputWorkload(statisticsWorkload);
			} else if (WorkloadType.强制补录.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendForceInputWorkload(statisticsWorkload);
			}else if (WorkloadType.对账单补录.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendBeBalInputWorkload(statisticsWorkload);
			}else if (WorkloadType.对账单扫描.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendBeBalScanWorkload(statisticsWorkload);
			} else if (WorkloadType.打包.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendPackedWorkload(statisticsWorkload);
			} else if (WorkloadType.入箱.getCode().equals(statisticsWorkload.getWorkType())) {
				statisticsWorkloadWrapper.appendInBoxWorkload(statisticsWorkload);
			}
		}
		//添加机构小计
		List<StatisticsWorkloadWrapper> resultWrappers= addOrganCount(wrappers);
		return resultWrappers;

	}
	//添加机构合计
	public static List<StatisticsWorkloadWrapper> addOrganCount(List<StatisticsWorkloadWrapper> wrappers){
		List<StatisticsWorkloadWrapper> resultWrappers = new ArrayList<StatisticsWorkloadWrapper>();
		Map<String,StatisticsBpoWorkloadWrapper> organWraperMap = new HashMap<String,StatisticsBpoWorkloadWrapper>();
		Map<String,Integer> organTotalMap = new HashMap<String,Integer>();
		StatisticsBpoWorkloadWrapper orgWorkloadWrapper = null;
		for(StatisticsWorkloadWrapper wrapper :wrappers){
			StatisticsBpoWorkloadWrapper pojo = (StatisticsBpoWorkloadWrapper)wrapper;
			if(organWraperMap.containsKey(pojo.getOrganCode())){
				orgWorkloadWrapper.addPropertyNum(pojo);
				int organTotal = organTotalMap.get(pojo.getOrganCode());
				organTotalMap.remove(pojo.getOrganCode());
				organTotalMap.put(pojo.getOrganCode(),organTotal+1);
			}else{
				orgWorkloadWrapper = new StatisticsBpoWorkloadWrapper(pojo.getOrganCode(),pojo.getOrganName(),
						pojo.getUserCode(),pojo.getUserName());
				BeanUtils.copyBeanProp(orgWorkloadWrapper,pojo);
				orgWorkloadWrapper.setUserCode("小计");
				orgWorkloadWrapper.setUserName(":");
				organWraperMap.put(pojo.getOrganCode(),orgWorkloadWrapper);
				organTotalMap.put(pojo.getOrganCode(),1);
			}
		}
		//合计
		StatisticsBpoWorkloadWrapper totalWorkloadWrapper = new StatisticsBpoWorkloadWrapper("","","合计",":");
		for(Map.Entry entry:organWraperMap.entrySet()){
			StatisticsBpoWorkloadWrapper pojo = (StatisticsBpoWorkloadWrapper)entry.getValue();
			totalWorkloadWrapper.addPropertyNum(pojo);
		}
		//机构小计
		for(int i=0;i<wrappers.size();i++){
			StatisticsWorkloadWrapper wrapper = wrappers.get(i);
			String organ = wrapper.getOrganCode();
			int index = organTotalMap.get(organ)-1;
			resultWrappers.add(wrapper);
			if(i==index){
				resultWrappers.add(organWraperMap.get(organ));
			}
		}
		resultWrappers.add(totalWorkloadWrapper);
		return resultWrappers;
	}
	public static List<StatisticsWorkloadWrapper> toStatisticsAduitWrapperList(
			List<StatisticsWorkload> statisticsWorkloads, String statType) {
		List<StatisticsWorkloadWrapper> wrappers = new ArrayList<StatisticsWorkloadWrapper>();
		String key = null;
		StatisticsAuditWorkloadWrapper statisticsWorkloadWrapper = null;
		for (StatisticsWorkload statisticsWorkload : statisticsWorkloads) {
			
			String organTellerString = statisticsWorkload.getOrganTellerString();
			
			if (organTellerString.equals(key) == false) {
				key = organTellerString;
				statisticsWorkloadWrapper = new StatisticsAuditWorkloadWrapper(
						statisticsWorkload.getOrganCode(), 
						statisticsWorkload.getOrganName(), 
						statisticsWorkload.getUserCode(), 
						statisticsWorkload.getUserName());
                
				wrappers.add(statisticsWorkloadWrapper);
			}
//			if(WorkloadType.重点监督.getCode().equals(statisticsWorkload.getWorkType())){
//				statisticsWorkloadWrapper.appendImpAuditWorkload(statisticsWorkload);
//			}
//			else if(WorkloadType.人工选择监督.getCode().equals(statisticsWorkload.getWorkType())){
//				statisticsWorkloadWrapper.appendManualSelectAuditWorkload(statisticsWorkload);
//			}
		}
		
		return wrappers;

	}
	

	public void appendForceInputWorkload(StatisticsWorkload statisticsWorkload) {

	}

	public void appendInputWorkload(StatisticsWorkload statisticsWorkload) {

	}

	public void appendScanWorkload(StatisticsWorkload statisticsWorkload) {

	}

	public void appendImpAuditWorkload(StatisticsWorkload statisticsWorkload) {

	}

	public void appendManualSelectAuditWorkload(StatisticsWorkload statisticsWorkload) {

	}

	public String getOrganCode() {
		return organCode;
	}

	public void setOrganCode(String organCode) {
		this.organCode = organCode;
	}

	public String getOrganName() {
		return organName;
	}

	public void setOrganName(String organName) {
		this.organName = organName;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}
